package com.shelpe.services.scheduler.item.adgroup;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;

import com.shelpe.services.scheduler.dto.AdgroupsDto;
import com.shelpe.services.scheduler.mongorepository.UnrecoverableErrorRepository;
import com.shelpe.services.scheduler.po.Campaigns;
import com.shelpe.services.scheduler.po.SyncLog;
import com.shelpe.services.scheduler.syncclient.AdgroupClient;
import com.shelpe.services.scheduler.util.Util;


public class AdgroupProcessor implements ItemProcessor<Campaigns, SyncLog>{

	private static final Logger logger = LoggerFactory.getLogger(AdgroupProcessor.class);
	
	@Autowired
	private AdgroupClient adClient;

	@Autowired
	private UnrecoverableErrorRepository errorRep;
	
	@Override
	public SyncLog process(Campaigns item) throws Exception {
		// TODO Auto-generated method stub
	
		long userId = item.getUserId();
		if(errorRep.countByUserId(userId) > 0){
			return null;
		}
		long campaignId = item.getCampaignId();
		AdgroupsDto dto = adClient.syncAdgroupsByUserId(userId, campaignId);	
		
		return Util.convert(dto);
	}
}